package com.luo.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.luo.config.Log;
import com.luo.pojo.User;
import com.luo.service.user.UserService;
import com.luo.utils.GeneratorUserAvatarUtil;
import com.luo.utils.JwtUtil;
import com.luo.utils.PasswordEncoderUtil;
import com.luo.utils.RandomFileNameUtil;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.Objects;

@RestController

public class RegController {
    @Resource
    private UserService userService;

    @PostMapping("/reg/hasUser")
    public boolean hasUser(@RequestBody HashMap<String,String> user, HttpServletRequest request){
        return userService.hasUser(user.get("username"));
    }

    @PostMapping("/hasPhone")
    public boolean hasPhone(@RequestBody HashMap<String,String> phone,HttpServletRequest request){
        return userService.hasPhone(phone.get("user"));
    }

    @Log(logStr = "新用户注册")
    @PostMapping("/reg")
    public String regUser(@RequestParam(value = "user",required = false)String users,
                        @RequestParam(value = "file",required = false) MultipartFile file,HttpServletRequest request)throws IOException {
        //System.out.println(users);
        //System.out.println(file.getOriginalFilename());
        JSONObject map = JSON.parseObject(users);
        //System.out.println(map.get("username"));
        User user = new User();
        user.setUsername(map.getString("username"));
        user.setPhoneNumber(map.getString("user"));
        user.setPassword(PasswordEncoderUtil.encoder(map.getString("pass")));

        if(file!=null){
            String name = RandomFileNameUtil.generateRandomFileName(Objects.requireNonNull(file.getOriginalFilename()));
            user.setAvatar(name);
            InputStream in = file.getInputStream();
            File mkdir = null;
            if(System.getProperty("os.name").toLowerCase().startsWith("win")){
                mkdir = new File(GeneratorUserAvatarUtil.WIN_IMG_PATH);
            }else{
                mkdir = new File(GeneratorUserAvatarUtil.LINUX_IMG_PATH);
            }
            if(!mkdir.exists()){
                mkdir.mkdirs();
            }
            //定义输出流，将文件写入硬盘
            FileOutputStream os = new FileOutputStream(mkdir.getPath()+"/"+name);
            int len = 0;
            while((len=in.read())!=-1){
                os.write(len);
            }
            os.flush();
            in.close();
            os.close();
            userService.userReg(user);
        }else{
            String s = GeneratorUserAvatarUtil.generateImg(map.getString("username"), map.getString("username"), "s");
            user.setAvatar("/"+s+".jpg");
            userService.userReg(user);
        }
        Map<String,String> m = new HashMap<>();
        m.put("user",map.getString("username"));
        return JwtUtil.setToken(m);
    }
}
